// 集合所有的 cookie & storage 相关的方法，方便名称统一
import {
    setCookie,
    getCookie,
    setNormalCookie,
    getNormalCookie
} from '@common/cookie';
import {
    envTarget
} from '@common/environment';
import logger from '@common/logger';

const APP_ID = 'app_id';

/**
 * 设置应用appid
 * @param {String} appId 应用用的appid
 */
export const setAppIdCookie = (appId) => {
    setCookie(APP_ID, appId, 1);
};

/**
 * 获取系统appid
 */
export const getAppIdCookie = () => {
    return getCookie(APP_ID);
};

const AUTH_APP_ID = 'auth_app_id';

/**
 * 设置应用mini_appid
 * @param {String} appId 应用的mini_appid
 */
export const setAuthAppIdCookie = (appId) => {
    setCookie(AUTH_APP_ID, appId, 1);
};

/**
 * 获取系统mini_appid
 */
export const getAuthAppIdCookie = () => {
    return getCookie(AUTH_APP_ID);
};

const COMPONENT_APP_ID = 'component_app_id';

/**
 * 设置第三方id
 * @param {String} componentAppId 应用第三发id
 */
export const setComponentAppId = (componentAppId) => {
    setCookie(COMPONENT_APP_ID, componentAppId, 1);
};

/**
 * 获取第三方id
 */
export const getComponentAppId = () => {
    return getCookie(COMPONENT_APP_ID);
};

const BRAND_ID = 'brand_id';

/**
 * 设置体系id
 * @param {String} brandId 体系id
 */
export const setBrandIdCookie = (brandId) => {
    setCookie(BRAND_ID, brandId, 1);
};

/**
 * 获取体系id
 */
export const getBrandIdCookie = () => {
    return parseInt(getCookie(BRAND_ID));
};

const BRAND_NAME = 'brand_name';

/**
 * 设置体系名称
 * @param {String} brandName 体系名称
 */
export const setBrandNameCookie = (brandName) => {
    setCookie(BRAND_NAME, brandName, 1);
};

/**
 * 获取体系名称
 */
export const getBrandNameCookie = () => {
    return getCookie(BRAND_NAME);
};

const BRAND_OPERATION_MODE = 'brand_operation_mode';

/**
 * 设置品牌模式
 * @param {Number} mode 模式
 */
export const setBrandOperationMode = (mode) => {
    setCookie(BRAND_OPERATION_MODE, mode);
};

/**
 * 获取品牌模式
 */
export const getBrandOperationMode = () => {
    return parseInt(getCookie(BRAND_OPERATION_MODE));
};

const BRAND_ALLOW_BOOKING = 'brand_allow_booking';

/**
 * 设置品牌是否支持预约
 * @param {Number} flag 是否支持
 */
export const setBrandAllowBooking = (flag) => {
    setCookie(BRAND_ALLOW_BOOKING, flag);
};

/**
 * 获取品牌是否支持预约
 */
export const getBrandAllowBooking = () => {
    return getCookie(BRAND_ALLOW_BOOKING);
};

const BRAND_BOOKING_DAYS = 'brand_booking_days';

/**
 * 设置品牌可预约天数
 * @param {Number} days 可预约天数
 */
export const setBrandBookingDays = (days) => {
    setCookie(BRAND_BOOKING_DAYS, days);
};

/**
 * 获取品牌可预约天数
 */
export const getBrandBookingDays = () => {
    return parseInt(getCookie(BRAND_BOOKING_DAYS));
};

const USER_TOKEN = 'user_token';

/**
 * 保存用户的 token 信息
 * @param {String} token 用户的token
 */
export const setUserTokenCookie = (token) => {
    setCookie(USER_TOKEN, token, 1);
};

/**
 * 获取用户的 token 信息
 */
export const getUserTokenCookie = () => {
    return getCookie(USER_TOKEN);
};

const USER_KEY = 'user_key';

/**
 * 保存用户信息的凭证
 * @param {String} key 用户信息临时存储凭证
 */
export const setUserKeyCookie = (key) => {
    setCookie(USER_KEY, key, 1);
};

/**
 * 获取用户信息的凭证
 */
export const getUserKeyCookie = () => {
    return getCookie(USER_KEY);
};

const USER_MEMBER_ID = 'user_member_id';

/**
 * 保存会员的id
 * @param {Number} memberId 会员 id
 */
export const setUserMemberIdCookie = (memberId) => {
    setCookie(USER_MEMBER_ID, memberId, 1);
};
/**
 * 获取保存的会员id
 */
export const getUserMemberIdCookie = () => {
    return parseInt(getCookie(USER_MEMBER_ID));
};

const USER_INFO = 'user_info';

/**
 * 保存用户信息
 * @param {Object} userInfo 用户信息
 */
export const setUserInfoCookie = (userInfo) => {
    logger.info('保存的会员信息为：', userInfo);
    setCookie(USER_INFO, userInfo, 1);
};

/**
 * 获取用户信息
 */
export const getUserInfoCookie = () => {
    return getCookie(USER_INFO).trim() ? JSON.parse(
        `${decodeURIComponent(getCookie(USER_INFO))}`) : '';
};

const USER_OPEN_ID = 'user_open_id';

/**
 * 保存微信 openId
 * @param {String} openId 用户微信 openId
 */
export const setUserOpenIdCookie = (openId) => {
    setCookie(USER_OPEN_ID, openId, 1);
};

/**
 * 获取微信 openId
 */
export const getUserOpenIdCookie = () => {
    return getCookie(USER_OPEN_ID);
};

const USER_G_OPEN_ID = 'user_g_open_id';

/**
 * 保存微信 openId
 * @param {String} openId 用户微信 openId
 */
export const setUserGOpenIdCookie = (gOpenId) => {
    setCookie(USER_G_OPEN_ID, gOpenId, 1);
};

/**
 * 获取微信 openId
 */
export const getUserGOpenIdCookie = () => {
    return getCookie(USER_G_OPEN_ID);
};

const MINI_ENV_TARGET = 'mini_env_target';

/**
 * 保存小程序的target
 * @param {String} openId 用户微信 openId
 */
export const setMiniEnvTargetCookie = (target) => {
    setCookie(MINI_ENV_TARGET, target, 1);
};

/**
 * 获取小程序的target
 */
export const getMiniEnvTargetCookie = () => {
    return getCookie(MINI_ENV_TARGET);
};

const ORDER_PAY_TYPE_ID = 'order_paytype_id';

/**
 * 保存订单支付的平台id
 * @param {String} openId 平台id
 */
export const setOrderPaytypeIdCookie = (paytypeId) => {
    setCookie(ORDER_PAY_TYPE_ID, paytypeId, 1);
};

/**
 * 获取订单支付的平台id
 */
export const getOrderPaytypeIdCookie = () => {
    return getCookie(ORDER_PAY_TYPE_ID);
};

const PAY_TYPE_AND_RESULT = 'pay_type_and_result';

/**
 * 设置支付类型和结果
 * @param {Object} result 支付结果和类型
 */
export const setPayTypeAndResult = (isPaySuccess, payType, isShengPay = false) => {
    const obj = {
        isPayBack: isPaySuccess,
        payType,
        isShengPay,
    };
    const result = JSON.stringify(obj || {});
    setCookie(PAY_TYPE_AND_RESULT, result, 1);
};

/**
 * 获取支付类型和结果，用户获取订单详情
 */
export const getPayTypeAndResult = () => {
    const val = getCookie(PAY_TYPE_AND_RESULT) || '';
    return val ? JSON.parse(val) : null;
};

/**
 * 清除支付结果和类型，屏蔽用户刷新页面到值错误
 */
export const clearPayTypeAndResult = () => {
    setCookie(PAY_TYPE_AND_RESULT, '', -1);
};

const IS_BROWSER_DEBUG = 'borwser_debug';

/**
 * 设置浏览器能够debug
 */
export const setBrowserDebug = () => {
    setNormalCookie(IS_BROWSER_DEBUG, true, 1);
};

/**
 * 获取是否能在浏览器debug
 */
export const getBrowserDebug = () => {
    return getNormalCookie(IS_BROWSER_DEBUG) || false;
};

const CODE_VALUE = 'code_value';

export const setCodeValue = (code) => {
    setCookie(CODE_VALUE, code, 1);
};

export const getCodeValue = () => {
    return getCookie(CODE_VALUE);
};

const UI_MODE = 'ui-mode';

export const setUiMode = (mode) => {
    setCookie(UI_MODE, mode);
};

export const getUiMode = () => {
    return getCookie(UI_MODE);
};

const UI_THEME = 'ui-theme';

export const setUiTheme = (theme) => {
    setCookie(UI_THEME, theme);
};

export const getUiTheme = () => {
    return getCookie(UI_THEME);
};

const UI_STYLE_MODE = 'ui-style-mode';

export const setUiStyleMode = (styleMode) => {
    setCookie(UI_STYLE_MODE, styleMode);
};

export const getUiStyleMode = () => {
    return getCookie(UI_STYLE_MODE);
};

const SELECT_DINETYPE = 'select_dinetype';

export const setDineTypeValue = (type) => {
    setCookie(SELECT_DINETYPE, type, 1);
};

export const getDineTypeValue = () => {
    return getCookie(SELECT_DINETYPE);
};

const MINI_VERSION = 'mini_version';

export const setMiniVersionValue = (version) => {
    setCookie(MINI_VERSION, version, 1);
};

export const getMiniVersionValue = () => {
    return parseInt(getCookie(MINI_VERSION) || 0);
};

const LOCATION_INFO = 'location_info';

export const setLocationValue = (location) => {
    setCookie(LOCATION_INFO, JSON.stringify(location), 1);
};

export const getLocationValue = () => {
    const val = getCookie(LOCATION_INFO);
    return val ? JSON.parse(val) : null;
};

const CONFIG_CART_GOODS = 'config_cart_goods';

export const setCartGoodsIds = (list) => {
    setCookie(CONFIG_CART_GOODS, list, 1);
};

export const getCartGoodsIds = () => {
    const data = getCookie(CONFIG_CART_GOODS);
    return data.split(',').filter(a => a); // .filter(a => a) 去除空元素[""]=>[]
};

/**
 * 清除所有的cookie
 */
export const clearAllCookies = () => {
    var rs = document.cookie.match(new RegExp('([^ ;][^;]*)(?=(=[^;]*)(;|$))', 'gi'));
    const scen = envTarget();
    // 删除所有cookie
    for (var i in rs) {
        if (rs[i].indexOf(scen) > -1) {
            document.cookie = rs[i] + '=;expires=Mon, 26 Jul 1997 05:00:00 GMT; path=/; ';
        }
    }
};
